package com.zjfgz.lib_kt_com.user

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.zjfgz.lib_kt_com.R
import com.zjfgz.lib_kt_com.base.BaseActivity
import com.zjfgz.lib_kt_com.base.BaseApp
import kotlinx.android.synthetic.main.activity_yinsi.*
import java.io.IOException

open class XieyiActivity : BaseActivity() {

    companion object{
        fun showXieYi(context: Context,clazz: Class<out XieyiActivity>){
            val intent = Intent(context,clazz)
            intent.putExtra("title","用户协议")
            intent.putExtra("path","xieyi.txt")
            context.startActivity(intent)
        }
        fun showYinSi(context: Context ,clazz: Class<out XieyiActivity>){
            val intent = Intent(context,clazz)
            intent.putExtra("title","隐私政策")
            intent.putExtra("path","yinsi.txt")
            context.startActivity(intent)
        }

        fun getFileText(
            context: Context,
            assetsPath: String?,
            s: String
        ): String? {
            try {
                val `in` = context.assets.open(assetsPath!!)
                val size = `in`.available()
                val buffer = ByteArray(size)
                `in`.read(buffer)
                `in`.close()
                return String(buffer)
            } catch (e: IOException) {
                e.printStackTrace()
            }
            return null
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_yinsi)
        toolbar.setNavigationOnClickListener {
            finish()
        }
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back)
        toolbar.title = intent.getStringExtra("title")
        val textFile = intent.getStringExtra("path")
        var content: String =
            getFileText(this, textFile, "utf-8")!!
        content = content.replace("***", BaseApp.instance.getAppName())
            .replace("###公司", BaseApp.instance.getCompanyName())
        tv_content.text = content
    }
}